$(function () {

    var table = $('#table_id_example').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        "scrollY": "550px",
        "scrollCollapse": true,
        "scrollX": true,
        ajax:{
            url:'/materiel/get-list',
            type:'post',
            data:function(d){
                d.searchName = $('#searchName').val();
                d.searchCode = $('#searchCode').val();
                d.warehouseId = $('#warehouseId').val();
                d.saleFlg = $('#saleFlg').val();
                d.useFlg = $('#useFlg').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": false},
            { "data": "materielName", "bSortable": false },
            { "data": "materielCode", "bSortable": false },
            //{ "data": "supplierName", "bSortable": false },
            { "data": "warehouseName", "bSortable": false },
            { "data": "specDesc", "bSortable": false },
            { "data": "packDesc", "bSortable": false },
            { "data": "purchaseUnit", "bSortable": true },
            { "data": "orderUnit", "bSortable":false },
            { "data": "convertUnit", "bSortable":false },
            { "data": "useUnit", "bSortable":false },
            { "data": "useDesc", "bSortable":false },
            { "data": "taxRate", "bSortable":false },
            { "data": "safeDay", "bSortable":false },
            { "data": "referPrice", "bSortable":false },
            { "data": "salePrice", "bSortable":false },
            { "data": "sort", "bSortable":false },
            { "data": "storeName", "bSortable":false },
            { "data": "safeStore", "bSortable": false },
            { "data": "storeFlg", "bSortable":false },
            { "data": "saleFlg", "bSortable":false },
            { "data": "useFlg", "bSortable":false },
        ],
        columnDefs: [
            {
                "targets": [0],
                "render": function(data, type, full) {
                    var html = '<a onclick="javascript:window.open(\'/materiel/detail?id='+full.id+'\',\'_blank\')">修改</a>';
                    return html;
                }
            },
            {
                "targets": [-1],
                "render": function(data, type, full) {
                    return data==1 ? '启用' : '禁用';
                }
            },
            {
                "targets": [-2],
                "render": function(data, type, full) {
                    return data==1 ? '上架' : '下架';
                }
            },
            {
                "targets": [-3],
                "render": function(data, type, full) {
                    return data==1 ? '是' : '否';
                }
            }
        ],
        //order: [[ 7, "desc" ]]
    });

    $("#select_button").on('click', function () {
        table.ajax.reload();
    });

    $("#reset_button").on('click', function () {
        $('#searchName').val('');
        $('#searchCode').val('');
        $('#warehouseId').val('');
        $('#saleFlg').val('1');
        $('#useFlg').val('1');
        table.ajax.reload();
    });

    $('#export_button').on('click',function () {
        var params = 'searchName=' + $('#searchName').val();
        params += '&searchCode=' + $('#searchCode').val();
        params += '&warehouseId=' + $('#warehouseId').val();
        params += '&saleFlg=' + $('#saleFlg').val();
        params += '&useFlg=' + $('#useFlg').val();
        params += '&v=' + Math.random();

        location.href = encodeURI('/materiel/export?' + params);
    })
})